package com.hphs.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hphs.dao.CategoryDao;
import com.hphs.dao.ProductDao;
import com.hphs.info.PageInfo;
import com.hphs.model.Category;
import com.hphs.model.CategoryQuery;
import com.hphs.model.Product;
import com.hphs.model.ProductQuery;
import com.hphs.threadlocal.LocalPageInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//业务逻辑处理层
@Service
public class ProductService {

    //使用数据接口
    @Autowired
    private ProductDao productDao;
    //查询所有数据
    public List<Product> select(ProductQuery query){
        if(query.getPageNum()!=null && query.getPageSize()!=null){
            PageHelper.startPage(query.getPageNum(),query.getPageSize());
        }
        List<Product> select = productDao.select(query);
        if(select instanceof Page){
            Page page= (Page) select;
            long total = page.getTotal();
            LocalPageInfoUtil.set(PageInfo.builder().total(total).build());
//            System.out.println(total);
        }
        return select;
    }

    public Integer insert(Product product){
        return productDao.insert(product);
    }

    public Integer update(Product product){
        return  productDao.update(product);
    }
    // 软删除
    public Integer updateStatus(Integer id){
        return productDao.updateStatus(id,0);
    }
}
